<!-- CountersignContract.vue -->
<template>
  <div>
    <el-card class="box-card">
      <h2>会签合同</h2>
      <el-form :model="countersignForm" ref="countersignForm" :rules="rules" label-width="120px">
        <el-form-item label="合同名称：" prop="name">
          <el-input v-model="countersignForm.name" readonly></el-input>
        </el-form-item>
        <el-form-item label="会签意见：" prop="opinion">
          <el-input v-model="countersignForm.opinion" type="textarea" :rows="10"></el-input>
        </el-form-item>
        <el-form-item label="合同附件：">
          <el-upload
            action=""
            list-type="text"
            :file-list="countersignForm.attachments"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            disabled
          >
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      countersignForm: {
        name: this.$route.params.contractName,
        opinion: '',
        attachments: []
      },
      rules: {
        opinion: [{ required: true, message: '请输入会签意见', trigger: 'blur' }]
      }
    };
  },

  methods: {

    handlePreview(file) {
      window.open(file.url);
    },
    handleRemove(file, fileList) {
      // Removing is disabled in this case, but you can implement logic if needed
    },
    submitForm() {
      this.$refs.countersignForm.validate((valid) => {
        if (valid) {
          const contractName = this.$route.params.contractName;
          this.axios.post(`/api/contract/countersign/${contractName}`, {
            opinion: this.countersignForm.opinion
          })
          .then(() => {
            this.$message({
              message: '会签意见提交成功',
              type: 'success'
            });
            this.$router.push('/countersign_contract'); // 返回待会签合同列表
          })
          .catch(error => {
            console.error('Error submitting countersign opinion:', error);
            this.$message.error('会签意见提交失败');
          });
        } else {
          this.$message.error('请填写会签意见');
        }
      });
    },
    resetForm() {
     this.countersignForm = {
        name: this.$route.params.contractName,
        opinion: '', // 设置为空或默认值
        attachments: [] // 设置为空数组
      };

    }
  }
};
</script>

<style scoped>
.box-card {
  margin: auto;
  width: 600px;
}
.el-form-item {
  margin-bottom: 20px;
}
</style>
